<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>vue从创建到挂载的过程 | 掏钱的机器猫</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/vue-admin-ui/assets/logo.png">
    <meta name="description" content="掏钱的机器猫的个人博客,记录随笔与学习笔记，个人项目等">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/vue-admin-ui/assets/css/0.styles.fb55c927.css" as="style"><link rel="preload" href="/vue-admin-ui/assets/js/app.21d1badc.js" as="script"><link rel="preload" href="/vue-admin-ui/assets/js/3.d02ddc64.js" as="script"><link rel="preload" href="/vue-admin-ui/assets/js/1.38c8ef5a.js" as="script"><link rel="preload" href="/vue-admin-ui/assets/js/34.30a7370c.js" as="script"><link rel="preload" href="/vue-admin-ui/assets/js/8.79ebffb7.js" as="script"><link rel="prefetch" href="/vue-admin-ui/assets/js/10.d9a14dc0.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/11.284df538.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/12.530bdb86.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/13.3dde5c1a.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/14.e18000c4.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/15.65b98979.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/16.953b2589.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/17.d6be13fa.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/18.a10ec5a3.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/19.f2ef083a.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/20.9e024a1a.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/21.b818c6cd.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/22.f3716a62.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/23.42bfb2a3.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/24.0382e6dc.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/25.9b1cf793.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/26.626abbca.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/27.d6b1a7e8.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/28.7381bce0.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/29.65b69672.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/30.70062600.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/31.036bec0e.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/32.1ec26c99.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/33.69485516.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/35.1e72f74a.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/36.a6aaafe3.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/4.984f5e20.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/5.2cbf0f84.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/6.eab8daa0.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/7.41bb2a3c.js"><link rel="prefetch" href="/vue-admin-ui/assets/js/9.e9ae19e3.js">
    <link rel="stylesheet" href="/vue-admin-ui/assets/css/0.styles.fb55c927.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-2d5f533b><div data-v-2d5f533b><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-2d5f533b data-v-2d5f533b><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-64685f0e data-v-2d5f533b data-v-2d5f533b><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>掏钱的机器猫</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>Ethan</span>
              
            <span data-v-64685f0e>2018 - </span>
            2022
          </a></span></div></div> <div class="hide" data-v-2d5f533b><header class="navbar" data-v-2d5f533b><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/vue-admin-ui/" class="home-link router-link-active"><img src="/vue-admin-ui/assets/logo.png" alt="掏钱的机器猫" class="logo"> <span class="site-name">掏钱的机器猫</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-document"></i>
        插件文档
      </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vue-admin-ui/category/vue-admin-ui-lib/" class="nav-link"><i class="iconfont undefined"></i>
    vue-admin-ui-lib
  </a></li><li class="dropdown-item"><!----> <a href="/vue-admin-ui/category/submit-valid/" class="nav-link"><i class="iconfont undefined"></i>
    submit-valid
  </a></li><li class="dropdown-item"><!----> <a href="/vue-admin-ui/category/open-utils/" class="nav-link"><i class="iconfont undefined"></i>
    open-utils
  </a></li></ul></div></div><div class="nav-item"><a href="/vue-admin-ui/handle/handle/method.html" class="nav-link"><i class="iconfont undefined"></i>
    手写代码
  </a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
        个人站点
      </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/ethan6" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
    Gitee
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://github.com/Ethan66" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
    Github
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <a href="https://github.com/Ethan66/vue-admin-ui" target="_blank" rel="noopener noreferrer" class="repo-link"><i class="iconfont reco-github"></i>
      Github
      <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask" data-v-2d5f533b></div> <aside class="sidebar" data-v-2d5f533b><div class="personal-info-wrapper" data-v-ca798c94 data-v-2d5f533b><img src="/vue-admin-ui/assets/logo.png" alt="author-avatar" class="personal-img" data-v-ca798c94> <h3 class="name" data-v-ca798c94>
    Ethan
  </h3> <div class="num" data-v-ca798c94><div data-v-ca798c94><h3 data-v-ca798c94>24</h3> <h6 data-v-ca798c94>文章</h6></div> <div data-v-ca798c94><h3 data-v-ca798c94>11</h3> <h6 data-v-ca798c94>标签</h6></div></div> <hr data-v-ca798c94></div> <nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-document"></i>
        插件文档
      </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/vue-admin-ui/category/vue-admin-ui-lib/" class="nav-link"><i class="iconfont undefined"></i>
    vue-admin-ui-lib
  </a></li><li class="dropdown-item"><!----> <a href="/vue-admin-ui/category/submit-valid/" class="nav-link"><i class="iconfont undefined"></i>
    submit-valid
  </a></li><li class="dropdown-item"><!----> <a href="/vue-admin-ui/category/open-utils/" class="nav-link"><i class="iconfont undefined"></i>
    open-utils
  </a></li></ul></div></div><div class="nav-item"><a href="/vue-admin-ui/handle/handle/method.html" class="nav-link"><i class="iconfont undefined"></i>
    手写代码
  </a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
        个人站点
      </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/ethan6" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
    Gitee
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://github.com/Ethan66" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
    Github
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <a href="https://github.com/Ethan66/vue-admin-ui" target="_blank" rel="noopener noreferrer" class="repo-link"><i class="iconfont reco-github"></i>
      Github
      <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span></span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vue-admin-ui/vue/vue/compile.html" class="sidebar-link">compile</a></li><li><a href="/vue-admin-ui/vue/vue/computed.html" class="sidebar-link">computed</a></li><li><a href="/vue-admin-ui/vue/vue/data.html" class="sidebar-link">依赖收集</a></li><li><a href="/vue-admin-ui/vue/vue/lifeCycle.html" aria-current="page" class="active sidebar-link">vue从创建到挂载的过程</a></li><li><a href="/vue-admin-ui/vue/vue/mixins.html" class="sidebar-link">mixins</a></li><li><a href="/vue-admin-ui/vue/vue/watch.html" class="sidebar-link">watch</a></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-64685f0e data-v-2d5f533b><h3 class="title" style="display:none;" data-v-64685f0e data-v-64685f0e>vue从创建到挂载的过程</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-64685f0e data-v-64685f0e><input type="password" value="" data-v-64685f0e> <span data-v-64685f0e>Konck! Knock!</span> <button data-v-64685f0e>OK</button></label> <div class="footer" style="display:none;" data-v-64685f0e data-v-64685f0e><span data-v-64685f0e><i class="iconfont reco-theme" data-v-64685f0e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-64685f0e>vuePress-theme-reco</a></span> <span data-v-64685f0e><i class="iconfont reco-copyright" data-v-64685f0e></i> <a data-v-64685f0e><span data-v-64685f0e>Ethan</span>
              
            <span data-v-64685f0e>2018 - </span>
            2022
          </a></span></div></div> <div data-v-2d5f533b><main class="page"><div class="page-title" style="display:none;"><h1 class="title">vue从创建到挂载的过程</h1> <div data-v-3b7f5bdf><i class="iconfont reco-account" data-v-3b7f5bdf><span data-v-3b7f5bdf>Ethan</span></i> <i class="iconfont reco-date" data-v-3b7f5bdf><span data-v-3b7f5bdf>2020-10-23</span></i> <!----> <i class="iconfont reco-tag tags" data-v-3b7f5bdf><span class="tag-item" data-v-3b7f5bdf>vue2源码</span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h1 id="vue从创建到挂载的过程"><a href="#vue从创建到挂载的过程" class="header-anchor">#</a> vue从创建到挂载的过程</h1> <h2 id="初始化vue实例"><a href="#初始化vue实例" class="header-anchor">#</a> 初始化vue实例</h2> <p><a href="https://nlrx-wjc.github.io/Learn-Vue-Source-Code/lifecycle/newVue.html#_1-%E5%89%8D%E8%A8%80" target="_blank" rel="noopener noreferrer">参考<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <ul><li>代码: vue-learn-code/Vue2.5.16/lifecycle.html。</li> <li>执行new Vue(options)，执行vue._init(options)。</li></ul> <h2 id="beforecreate之前"><a href="#beforecreate之前" class="header-anchor">#</a> beforeCreate之前</h2> <blockquote><p>初始化Vue实例时，执行vue._init()方法，调用mergeOptions()方法，校验配置的options的规范，如果配置项里有mixins，递归调用mergeOptions方法，把mixins的配置项匹配给parent，再遍历配置的options，把每项与父级构造函数的options属性进行合并（如果childVal不存在，就返回parentVal,如果存在，就把childVal添加到parentVal后，比如生命钩子，所以mixins里的生命周期早于当前实例执行），将得到一个新的options选项赋值给$options属性。再执行beforeCreate钩子，此时还没有$el和$data。
所以beforeCreate之前是将options配置项检验合并。</p></blockquote> <h3 id="vue-init"><a href="#vue-init" class="header-anchor">#</a> vue._init()</h3> <ul><li>_init方法初始化一些属性，vm._uid（唯一id）, vm._self = vm, vm._isVue = true。</li> <li><a href="/vue-admin-ui/vue/lifeCycle.html#mergeoptions">调用mergeOptions()</a>，返回值给$options。</li> <li>调用initLifecycle(), 初始化vm.$parent, vm.$root, vm.$children, vm.$refs, vm._watcher等</li> <li><code>调用callHook(vm, 'beforeCreate')</code>: 因为mergeOptions已经将parent.beforeCreate和options.beforeCreate都合成一个数组$options.beforeCreate了，所以遍历调用就行了</li></ul> <h3 id="mergeoptions"><a href="#mergeoptions" class="header-anchor">#</a> mergeOptions()</h3> <ul><li>mergeOptions(resolveConstructorOptions(vm.constructor), options || {}, vm)</li> <li>参数：resolveConstructorOptions(vm.constructor): 解析constructor上的options属性，一般为{}，当做parent</li> <li>参数：options: 配置options</li> <li>校验options规范：校验子组件components的name规范，props是否是对象数组等</li> <li>合并mixins: 执行mergeOptions(parent, child.mixins[i], vm)，返回值传给parent</li> <li>先遍历parent的属性key，执行mergeField(key)</li> <li>后遍历options的属性key，将parent不存在的属性key进行mergeField(key)</li></ul> <h4 id="mergefield-key"><a href="#mergefield-key" class="header-anchor">#</a> mergeField(key)</h4> <ul><li>strats变量：策略者模式，每个key存着相对应的方法，比如strats.el()，strats.data()。取出来赋值给strat变量</li> <li>执行方法strat()，将parent的key属性的value和options的key属性的value进行合并，返回的值赋值给新的options的属性key</li> <li>最后return options，跳出mergeOptions()方法</li></ul> <div class="code-wrap"><!----> <div class="code--demo"><div class="code-content"><div>strats.data()：childVal为options.data, parentVal为parent.data</div> <div class="code-tips"></div></div></div> <div class="code--segment"><highlight-code lang="js">
    function () {
      return function mergedInstanceDataFn() {
        // instance merge
        var instanceData = typeof childVal === 'function'
            ? childVal.call(vm, vm)
            : childVal;
<div class="language- extra-class"><pre><code>    var defaultData = typeof parentVal === 'function'
        ? parentVal.call(vm, vm)
        : parentVal;

    if (instanceData) {
        return mergeData(instanceData, defaultData)
    } else {
        return defaultData
    }
 }
}
</code></pre></div></highlight-code></div> <div class="code--button">显示代码</div></div> <h2 id="created之前"><a href="#created之前" class="header-anchor">#</a> created之前</h2> <blockquote><p>调用initState()方法，处理$options里每个属性，先处理props，再给data实现数据响应，最后给computed，watch添加观察者。再执行created钩子。</p></blockquote> <h3 id="initinjections-vm"><a href="#initinjections-vm" class="header-anchor">#</a> initInjections(vm)</h3> <p>初始化provide和inject配置</p> <h3 id="initstate-vm"><a href="#initstate-vm" class="header-anchor">#</a> initState(vm)</h3> <ul><li>initProps(vm, opts.props): 判断是否有$options.props，添加观察者模式</li> <li>initMethods(vm, opts.methods): 判断是否有$options.methods, 初始化事件</li> <li><a href="/vue-admin-ui/vue/lifeCycle.html#initData">initData(vm)</a></li> <li>initComputed(vm, opts.computed): 判断是否有$options.computed，初始化计算属性</li> <li>initWatch(vm, opts.watch): 判断是否有$options.watch，进行初始化watch</li></ul> <h4 id="initdata-vm"><a href="#initdata-vm" class="header-anchor">#</a> initData(vm)</h4> <ul><li>判断$options.data是方法还是对象，对象直接给vm._data，否则执行$options.data.call(vm, vm), 返回给vm._data</li> <li>判断vm_data和$options.props和$options.methods属性是否重复，重复报警告</li> <li>遍历每个属性执行proxy(vm, '_data', key)，进行数据代理：vm.key指向vm._data.key</li> <li>对vm._data的每一个属性进行响应化</li></ul> <h3 id="initprovide-vm"><a href="#initprovide-vm" class="header-anchor">#</a> initProvide(vm)</h3> <h3 id="调用callhook-vm-created"><a href="#调用callhook-vm-created" class="header-anchor">#</a> 调用callHook(vm, 'created')</h3> <h2 id="beforemounted之前"><a href="#beforemounted之前" class="header-anchor">#</a> beforeMounted之前</h2> <blockquote><ol><li>vue有2个版本的代码：runtime only（只包含运行时版本）和runtime+compiler（同时包含编译器和运行时的完整代码）。他们的区别就是编译器，将template转化为render函数。实际开发用runtime only，同时借助webpack的vue-loader，通过这工具来将模板编译成render函数。</li> <li>所以两个版本区别在于：vue.$mount()方法是否将vue.prototype.$mount方法缓存起来，直接执行$mount的就是runtime only版本，否则就是完整版本。完整版先根据传入的el获取dom元素，判断有无配置render函数，没有的话判断配置是否有template模板，是字符串，或者是dom的直接获取innerHtml，没有配置template就是el作为innerHtml。将模板字符串编译成render函数。传给$options.render，再执行原始vue.prototype.$mount()函数。</li></ol></blockquote> <h3 id="vm-mount-vm-options-el"><a href="#vm-mount-vm-options-el" class="header-anchor">#</a> vm.$mount(vm.$options.el)</h3> <blockquote><p>vm.$mount为手动挂载函数</p></blockquote> <ul><li>query(el): 获取dom元素</li> <li>判断$ptions.render是否存在</li> <li>不存在$options.render，判断$options.template类型，是字符串（dom的id）或者dom元素，获取innerHTML；没有$options.template，获取$options.el外层的dom的innerHTML，获取template模板字符串</li> <li>不存在$options.render，将template模板字符串通过<code>compileToFunctions()</code>方法转为render函数，将转化后的render函数赋值给$options.render</li> <li>执行原先绑定在vue.prototype.$mount的方法，<code>mount.call(this, el)</code>，方法里有<code>callHook(vm, 'beforeMount')</code>，此时还没有跳出mount方法</li></ul> <h3 id="callhook-vm-beforemount"><a href="#callhook-vm-beforemount" class="header-anchor">#</a> callHook(vm, 'beforeMount')</h3> <h2 id="mounted之前"><a href="#mounted之前" class="header-anchor">#</a> mounted之前</h2> <ul><li>开始挂载前，创建new Watcher(vm, updateComponent)实例watcher，updateComponent为外部定义的方法，将它存在watcher.update中。</li> <li>执行updateComponent()，方法里先执行vm._render()，获取最新的Vnode节点树，同时获取了vm.data属性，vm.data的属性监听了watcher。此时还没跳出函数，再执行vm._update()方法，此时没有旧的Vnode节点树，直接更新DOM，完成第一次渲染。</li> <li>执行callHook(vm, 'mounted')</li></ul> <h2 id="beforeupdate"><a href="#beforeupdate" class="header-anchor">#</a> beforeUpdate</h2> <p>修改数据，触发watcher.update方法，判断vm._isMounted === true（已被挂载过了），执行callHook(vm, 'beforeUpdate')，再执行updateComponent()，将最新的Vnode节点树和上一次渲染的Vnode节点树对比，并更新DOM节点（即patch操作），再执行callHook(vm, 'updated')</p> <h2 id="beforedestroy"><a href="#beforedestroy" class="header-anchor">#</a> beforeDestroy</h2> <ul><li>执行vm.$destroy方法，执行callHook(vm, 'beforeDestroy')，没有跳出函数。</li> <li>判断当前实例有父级实例，并且父级实例没有被销毁且不是抽象组件，那么将当前实例从父级实例的$children属性中删除。</li> <li>移除依赖：遍历vm._watchers，调用每一个watcher的teardown方法。</li> <li>移除响应式数据的引用：vm._data.<strong>ob</strong>.vmCount--</li> <li>移除Vnode: vm.<strong>patch</strong>(vm._vnode, null)</li> <li>callHook(vm, 'destroyed')</li></ul></div> <footer class="page-edit" style="display:none;"><!----> <div class="last-updated"><span class="prefix">上次更新: </span> <span class="time">2022/7/8 上午10:51:20</span></div></footer> <!----> <!----> <!----></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/vue-admin-ui/assets/js/app.21d1badc.js" defer></script><script src="/vue-admin-ui/assets/js/3.d02ddc64.js" defer></script><script src="/vue-admin-ui/assets/js/1.38c8ef5a.js" defer></script><script src="/vue-admin-ui/assets/js/34.30a7370c.js" defer></script><script src="/vue-admin-ui/assets/js/8.79ebffb7.js" defer></script>
  </body>
</html>
